package com.yuchen.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单编号生成类
 */
public class OrderNo {

    private final static SimpleDateFormat FMT_FN = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    public static synchronized String createNo(String userId){
        if(StringUtils.isBlank(userId)){
            throw new RuntimeException("can not create orderNo for null user.");
        }
        StringBuilder no = new StringBuilder();
        Random random = new Random();
        long r = Math.round(random.nextDouble()*(1000000-100000)+100000);
        no.append(userId);
        no.append(FMT_FN.format(new Date()).substring(1));
        no.append(r);
        return no.toString();
    }

    public static synchronized String createNo(){
        StringBuilder no = new StringBuilder();
        Random random = new Random();
        long r = Math.round(random.nextDouble()*(1000000-100000)+100000);
        no.append(FMT_FN.format(new Date()));
        no.append(r);
        return no.toString();
    }

    public class RunableTest implements Runnable{
        Set set;
        RunableTest(Set set){
            this.set = set;
        }
        public void run() {
            //多用户并发测试
            set.add(OrderNo.createNo(Thread.currentThread().getName()));
            //单一用户并发测试
//            set.add(OrderNo.createNo("wjj"));
        }
    }

    public static void main(String[] args)throws Exception{
//        Set set = new HashSet<String>(200000);
//        for(int i = 0; i < 10000; i++){
//            set.add(createNo("1000"));
//        }
//        System.out.println(set.size());
//        OrderNo order = new OrderNo();
//        RunableTest test = order.new RunableTest(set);
//        Thread t = null;
//        for(int i = 0; i < 100000; i++){
//            t = new Thread(test);
//            t.start();
//            t.join();
//        }
//        System.out.println(set.size());
//        UUID uuid = UUID.randomUUID();
//        System.out.println(uuid.hashCode());
        System.out.println( OrderNo.createNo());
        System.out.println( OrderNo.createNo().length());

        Map map = new HashMap(2);
    }
}
